<template>
    <div id="v-right-c">
        <el-row class="e-row-r" type="flex" justify="space-between">
            <el-col :xs="24" :sm="24" :md="24" :lg="17" :xl="17" class="mb20-u">
                <div class="line-s">
                    <span>分类汇总</span>
                    <div style="width: 100%;height: 100%;" ref="ringChar"></div>
                </div>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="7" :xl="7" class="mb20-u">
                <ul class="legend-u">
                    <li v-for="(item,index) in liData" :key="index"><span class="legend-s" :style="{backgroundColor: item.color}"></span><span class="legend-t">{{item.name}}</span></li>
                </ul>
            </el-col>
        </el-row>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted , nextTick, watchEffect, reactive , toRaw } from 'vue';
import * as echarts from 'echarts';
import ecTheme from "/@/assets/echartsConfig/westeros.project.json";
export default defineComponent({
    name:'v-right-c',
    props: {
        rightCenterData: {
            type: String,
            default: ''
        },
    },
    setup(props){
        const ringChar:Ref = ref(null);
        const liData = ref();
        let colorArr = [
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
            'rgb(6,235,125)',
            'rgb(223,49,69)',
            'rgb(223,184,38)',
            'rgb(255,232,159)',
        ];
        let isFinish = false;

        echarts.registerTheme('westeros', ecTheme);
        const optionRing = () => {
            let chart: any; 
            chart = echarts.init(ringChar.value as HTMLElement, 'westeros');
            let option = {
                color: colorArr,
                tooltip: {
                    trigger: 'item',
                    position: 'top',
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['30%','60%'],
                        center: ['50%','40%'],
                        data: [
                            { value: 580, name: '工具摆放杂乱' },
                            { value: 735, name: '态度不良' },
                            { value: 1048, name: '其他原因' },
                        ],
                        labelLine:{
                            length1: 5,
                            length2: 22,
                        },
                        label: {
                            formatter: '{b}\n{d}%',
                            padding: [0,-20,0,-20],
                            color: '#FFFFFF',
                        },
                    }
                ]
            }
            let setTime = setInterval(() => {
                if(isFinish || liData.value){
                    let seriesData: any = [];
                    if(liData.value.length !== 0){
                        liData.value.map((i: any) => {
                            let seriesObj: any = {};
                            seriesObj.value = i.count;
                            seriesObj.name = i.name;
                            seriesData.push(seriesObj);
                        });
                    }
                    option.series[0].data = seriesData;
                    clearInterval(setTime);
                    return option && chart.setOption(option)
                }
            },20);
        }
        watchEffect(() => {
            let getData = props.rightCenterData;
            if(getData !== ''){
                let setArr = JSON.parse(getData);
                setArr.map((i: any, inx: any) => {
                    i.color = colorArr[inx];
                });
                console.log(setArr,'setArr')
                liData.value = setArr;
                isFinish = true;
                optionRing();
            }
        });
        onMounted(() => {
            isFinish = false;
            optionRing();
        })
        return {
            ringChar,
            liData,
        }
    }
})
</script>
<style lang="scss" scoped>
#v-right-c{
    width: 100%;
    height: 100%;
    ul,li{
        padding: 0;
        margin: 0;
        list-style: none;
    }
    .e-row-r{
        // height: 100%;
        height: 72%;
        width: 92%;
        margin: 6vh auto 0 auto;
        .mb20-u{
            height: 100%;
            .line-s{
                height: 100%;
                span{
                    display: block;
                    width: 100%;
                    text-align: center;
                    margin-bottom: 5px;
                }
            }
            .legend-u{
                margin-top: 2vw;
                li{
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    margin-bottom: 10px;
                    span{
                        display: block;
                    }
                    .legend-s-1{
                        background: rgb(6,235,125);
                    }
                    .legend-s-2{
                        background: rgb(223,49,69);
                    }
                    .legend-s-3{
                        background: rgb(223,184,38);
                    }
                    .legend-s{
                        width: 12px;
                        height: 12px;
                        margin-right: 5px;
                    }
                }
            }
        }
    }
}
</style>